home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / java / appleevent send and receive / source / aesend source / aesend.java next >
Encoding:
Java Source  |  2000-06-23  |  6.2 KB  |  180 lines

  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;
  3. import java.awt.AWTEventMulticaster;
  4.  
  5. import  com.apple.mrj.jdirect.PointerStruct;
  6.  
  7. /**
  8.  * Apple Worldwide Developer Technical Support
  9.  *
  10.  * Sample showing how to send and receive AppleEvents using JDirect 2.
  11.  *
  12.  * File: AESend.java
  13.  *
  14.  * This class contains the code needed to send an AppleEvent and
  15.  * to extract any reply information from the AppleEvent.
  16.  * The reply information is fired as an java.awt.event.ActionEvent
  17.  * to all registered ActionListeners.
  18.  *
  19.  * @author Levi Brown
  20.  * @author Michael Hopkins
  21.  * @author Apple Computer, Inc.
  22.  *
  23.  * Copyright ©1999 Apple Computer, Inc.
  24.  * All rights reserved.
  25.  *
  26.  * @version 1.0
  27.  * 4/15/1999 Shipped as 'AppleEvent Send and Receive' sample.
  28.  *
  29.  * You may incorporate this sample code into your applications without
  30.  * restriction, though the sample code has been provided "AS IS" and the
  31.  * responsibility for its operation is 100% yours.  However, what you are
  32.  * not permitted to do is to redistribute the source as "Apple Sample
  33.  * Code" after having made changes. If you're going to re-distribute the
  34.  * source, we require that you make it clear in the source that the code
  35.  * was descended from Apple Sample Code, but that you've made changes.
  36.  */
  37. public class AESend implements TypesConstants, AppleEventConstants, AEDataModelConstants, ErrorConstants
  38. {
  39.     /**
  40.      * The maximum length of the keyDirectObject parameter of the Apple Event
  41.      */
  42.     protected static final int kMaxTextSize    = 255;
  43.     /**
  44.      * The length of an OSType (int).
  45.      */
  46.     protected static final int kSizeOfOSType    = 4;    
  47.     /**
  48.      * The OSType (four character code) of the target application's signature.
  49.      * 'TSnd' in this case.
  50.      */
  51.     protected static final byte[] kTestTargetSig        = { (byte)'T',(byte)'R',(byte)'c',(byte)'v'}; //'TRcv'
  52.     /**
  53.      * The OSType (four character code) of the event class.
  54.      * 'Test' in this case.
  55.      */
  56.     protected static final int kTestClass    = 0x54657374; //'Test'
  57.     /**
  58.      * The OSType (four character code) of the event ID.
  59.      * 'TsID' in this case.
  60.      */
  61.     protected static final int kTestID        = 0x54734944; //'TsID'
  62.     /**
  63.      * A null callback handler for AESend
  64.      */
  65.     protected static final AEIdleClosureUPP kNoIdleProc = null;
  66.     /**
  67.      * A null callback handler for AESend
  68.      */
  69.     protected static final AEFilterClosureUPP kNoFilterProc = null;
  70.     /**
  71.      * The list of all registered ActionEvent listeners for this object.
  72.      * @see #addActionListener
  73.      * @see #removeActionListener
  74.      * @see #fireActionEvent
  75.      */
  76.     protected ActionListener actionListener = null;
  77.  
  78.     /**
  79.      * Sends a string to our target application via Apple Events.
  80.      * @param theText, the string to pass along to the target application.
  81.      * @exception if any problem occured while sending the AppleEvent.
  82.      */
  83.     public void sendAppleEvent(String theText) throws NativeException
  84.     {
  85.         AEAddressDescStruct    targetAddrDesc = new AEAddressDescStruct();
  86.         AppleEventStruct event = new AppleEventStruct();
  87.         AppleEventStruct reply = new AppleEventStruct();
  88.         short err;
  89.         int[] actualType = new int[1];
  90.         int[] actualSize = new int[1];
  91.         byte[] replyText = new byte[kMaxTextSize];
  92.         NativeException error = null;
  93.  
  94.         try
  95.         {
  96.             //Create a new descriptor using the target applications 4 character type
  97.             err = AEDataModelFunctions.AECreateDesc(typeApplSignature, kTestTargetSig, kSizeOfOSType, targetAddrDesc);
  98.             ErrorHandler.checkError(err, "Error returned by AECreateDesc()");
  99.  
  100.             //Create the apple event with the class, event id, and address of the apple event target. 
  101.             err = AEDataModelFunctions.AECreateAppleEvent(kTestClass, kTestID, targetAddrDesc, (short)kAutoGenerateReturnID, kAnyTransactionID, event);
  102.             ErrorHandler.checkError(err, "Error returned by AECreateAppleEvent()");
  103.  
  104.             if (theText != null && !theText.equals("") ) // if string is not null, and not empty
  105.             {
  106.  
  107.                 //Turn the String into a byte array
  108.                 byte[] theData = theText.getBytes();
  109.                 
  110.                 //Put the string into the direct object parameter
  111.                 err = AEDataModelFunctions.AEPutParamPtr(event, keyDirectObject, typeChar, theData, theData.length);
  112.                 ErrorHandler.checkError(err, "Error returned by AEPutParamPtr()");
  113.  
  114.                 //Send the apple event with an empty reply event. Wait for reply, user interaction allowed
  115.                 err = AppleEventFunctions.AESend(event, reply, kAEWaitReply + kAECanInteract + kAECanSwitchLayer, (short)kAENormalPriority, kAEDefaultTimeout, kNoIdleProc, kNoFilterProc);
  116.  
  117.                 if ( err == connectionInvalid )
  118.                 {
  119.                     throw new NativeException("The target application is not running.", err);
  120.                 }
  121.                 
  122.                 ErrorHandler.checkError(err, "Error returned by AESend()");
  123.                 
  124.                 //Retrieve keyDirectObject parameter from reply structure
  125.                 err = AEDataModelFunctions.AEGetParamPtr(reply, keyDirectObject, typeChar, actualType, replyText, kMaxTextSize, actualSize);
  126.  
  127.                 ErrorHandler.checkError(err, "Error returned by AEGetParamPtr()");
  128.  
  129.                 //Create a String from the byte array of returned data.
  130.                 String text = new String(replyText, 0, actualSize[0]);
  131.                 
  132.                 //Fire an ActionEvent with the reply data so registered ActionListeners can see the AppleEvent being received.
  133.                 fireActionEvent(text);
  134.             }
  135.         }
  136.         catch (NativeException exc)
  137.         {
  138.             error = exc;
  139.         }
  140.         finally
  141.         {
  142.             AEDataModelFunctions.AEDisposeDesc(targetAddrDesc);
  143.             AEDataModelFunctions.AEDisposeDesc(event);
  144.             AEDataModelFunctions.AEDisposeDesc(reply);
  145.         }
  146.         
  147.         if (error != null)
  148.             throw error;
  149.     }
  150.     
  151.     /**
  152.      * Adds the specified action listener to receive action events from this object.
  153.      * @param l the action listener
  154.      */
  155.     public void addActionListener(ActionListener l)
  156.     {
  157.         actionListener = AWTEventMulticaster.add(actionListener, l);
  158.     }
  159.  
  160.     /**
  161.      * Removes the specified action listener so it no longer receives
  162.      * action events from this object.
  163.      * @param l the action listener
  164.      */
  165.     public void removeActionListener(ActionListener l)
  166.     {
  167.         actionListener = AWTEventMulticaster.remove(actionListener, l);
  168.     }
  169.  
  170.     /**
  171.      * Fire an action event to the listeners.
  172.      * @param the action command associated with the event.
  173.      * In this case it will be the text from the AppleEvent.
  174.      */
  175.     protected void fireActionEvent(String message)
  176.     {
  177.         if (actionListener != null)
  178.             actionListener.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, message));
  179.     }
  180. }